package com.baidu.Chapter03;/*
 * classname:Login
 * Package:com.baidu.Chapter03
 * Creat 2024/3/30 10:10
 * */

import com.sun.tools.javac.Main;

import java.util.Objects;
import java.util.Scanner;

public class Login {
    public static void main(String[] args) {
        CustomerView cust=new CustomerView();
       cust.Loginview();
    }
}
class Customer{
    public String count;
    public String password;

    public Customer() {
    }

    public Customer(String count, String password) {
        this.count = count;
        this.password = password;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Customer customer = (Customer) o;
        return Objects.equals(count, customer.count) && Objects.equals(password, customer.password);
    }

    @Override
    public int hashCode() {
        return Objects.hash(count, password);
    }
    //    public String getCount() {
//        return count;
//    }
//
//    public String getPassword() {
//        return password;
//    }
//
//    public void setCount(String count) {
//        this.count = count;
//    }
//
//    public void setPassword(String password) {
//        this.password = password;
//    }
}
class CustomerList{
    Customer[] customers;//记录已保存客户信息
    int total;//数组最大长度
    //初始化customers数组

    public CustomerList() {
    }

    public CustomerList(int totalCustomer){
        customers=new Customer[totalCustomer];
    }
//注册新用户
    public boolean addCustomer(Customer customer){
        if (total<customers.length){
            customers[total]=customer;
            total++;
            return true;
        }else {
            return false;
        }
    }
    public boolean login(Customer customer){
        for(int i=0;i<customers.length;i++){
            if (customers[i].equals(customer)){
                return true;
            }
        }
        return false;
    }
//遍历账户密码
//    public Customer[] getAllCustomers(){
//        Customer[] custs=new Customer[total];
//        for(int i=0; i<=total; i++){
//            custs[i]=customers[i];
//        }
//        return custs;
//    }


}
class CustomerView{
    CustomerList list=new CustomerList(10);
    public void Loginview(){
        int a;
        Scanner input=new Scanner(System.in);
        boolean istag=true;
        while (istag){
            System.out.println("欢迎来到登录/注册页面:");
            System.out.println("1.登录");
            System.out.println("2.注册");
            System.out.println("3.退出");
            System.out.println("请输入1/2/3:");
            a=input.nextInt();
            switch (a){
                case 1:
                    //登录
                    login();
                    break;
                case 2:
                    //注册
                    addNewCustomer();
                    break;
                case 3:
                    istag=false;
                    break;
                default:break;
            }
        }
    }

    public void addNewCustomer(){
        Scanner input=new Scanner(System.in);
        System.out.println("-----欢迎注册-----");
        System.out.println("账号:");
        String name=input.next();
        System.out.println("密码:");
        String password=input.next();
        Customer custs=new Customer(name,password);
        boolean isFlag=list.addCustomer(custs);
        if (isFlag){
            System.out.println("----添加成功------");
        }else{
            System.out.println("----添加失败------");
        }
    }
    public void login(){
        Scanner input=new Scanner(System.in);
        System.out.println("-----用户登录-----");
        System.out.println("账号:");
        String name=input.next();
        System.out.println("密码:");
        String password=input.next();
        Customer custs=new Customer(name,password);
        boolean isFlag=list.login(custs);
        if (isFlag){
            System.out.println("----登陆成功------");
        }else{
            System.out.println("----账号或密码错误！！！------");
        }

    }
}

